package com.xxg.renrenuser.service;

import com.xxg.renrencommon.domain.filedenum.userenum.BankCodeEnum;
import com.xxg.renrencommon.domain.result.BaseResult;
import com.xxg.renrencommon.exception.UserManagerException;
import com.xxg.renrencommon.exception.exceptionenum.UserExceptionCodeCodeEnum;
import com.xxg.renrenuser.dao.UserBankCardInfoDao;
import com.xxg.renrenuser.dao.bean.UserBankCardInfo;
import com.xxg.renrenuser.domain.result.BankAuthenticateResult;
import com.xxg.renrenuser.manager.AliCloudApi;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * 绑定银行卡接口实现
 *
 * @author xxg
 * @date 2020/11/6
 */
@Service
@AllArgsConstructor
public class UserBindBankCardServiceImpl implements UserBindBankCardService {
    private final AliCloudApi aliCloudApi;
    private final UserBankCardInfoDao userBankCardInfoDao;

    @Override
    public BaseResult<Void> bindBankCard(String userId, String cardNo, String idNo, String name) {
        // 查询是否重复绑定
        boolean flag = userBankCardInfoDao.findByCardNo(userId, cardNo);
        if (flag) {
            return new BaseResult<>();
        }
        BankAuthenticateResult result;
        try {
            result = aliCloudApi.bankAuthenticate3(cardNo, idNo, name);
        } catch (UserManagerException e) {
            return new BaseResult<>(UserExceptionCodeCodeEnum.BIND_BANK_CARD_ERROR, e.getMessage(), null);
        }
        if (!BankCodeEnum.valueContain(result.getBankCode())) {
            return new BaseResult<>(UserExceptionCodeCodeEnum.ONLY_4_BANK);
        }
        // 插入银行卡记录
        UserBankCardInfo userBankCardInfo = new UserBankCardInfo();
        String bindId = UUID.randomUUID().toString().replace("-", "");
        userBankCardInfo.setBindId(bindId);
        userBankCardInfo.setUserId(userId);
        userBankCardInfo.setIdNo(result.getIdNo());
        userBankCardInfo.setCardNo(result.getCardNo());
        userBankCardInfo.setUserName(result.getName());
        userBankCardInfo.setBankName(result.getBankName());
        userBankCardInfo.setBankCode(result.getBankCode());
        userBankCardInfo.setBankKind(result.getBankKind());
        userBankCardInfo.setBankType(result.getBankType());
        userBankCardInfoDao.insertSelective(userBankCardInfo);
        return new BaseResult<>();
    }

    @Override
    public BaseResult<Void> unBindBankCard(String userId, String bindId) {
        userBankCardInfoDao.deleteByUserIdBindId(userId, bindId);
        return new BaseResult<>();
    }

    @Override
    public BaseResult<List<UserBankCardInfo>> getBindBankCards(String userId) {
        List<UserBankCardInfo> cardInfos = userBankCardInfoDao.selectByUserId(userId);
        cardInfos.forEach(e -> {
            int len = e.getCardNo().length();
            String cardNo = "********" + e.getCardNo().substring(len - 4);
            e.setCardNo(cardNo);
        });
        return new BaseResult<>(cardInfos);
    }
}
